/**
 * 体验中心
 */
define(function (require, exports, module) {
    var angular = require('angular');
    var ui = require('ui');
    var util = require('util');
    var $ = require('jquery');
    require("angular-messages");
    require("pagination");
    require("angular-ui");
    var urlprefix = '/api/index.php?r=/exp-center/';


    angular.module('ExpCenter', ['ngMessages'])
        .factory('httpPost', ['$http', function ($http) {
            return function (url, data) {
                if (!data) {
                    data = {};
                }
                data = $.extend(data, {"USE_FORM_DATA": false});
                return $http.post(url, data);
            }
        }])
        .service('getExpCenterList', ['$q', 'httpPost', function ($q, httpPost) {
            return function (queryData) {
                var getParams = {
                    pageIndex: queryData.pageIndex,
                    pageSize: queryData.pageSize || 10
                };
                var deferred = $q.defer();
                httpPost(urlprefix + 'get-user-list&' + $.param(getParams), getParams)
                    .success(function (res) {
                        if (res.retCode == 0) {
                            deferred.resolve(res);
                        }
                    });
                return deferred.promise;
            }
        }])
        .service('resetPwdService', ['$q', 'httpPost', function ($q, httpPost) {
            return function (data) {
                var deferred = $q.defer();
                httpPost(urlprefix + 'update-pwd', data)
                    .success(function (res) {
                        if (res.retCode == 0) {
                            deferred.resolve(res);
                        }
                    });
                return deferred.promise;
            }
        }])
        .service('createAccountService', ['$q', 'httpPost', function ($q, httpPost) {
            return function (data) {
                var deferred = $q.defer();
                httpPost(urlprefix + 'add-user', data)
                    .success(function (res) {
                        if (res.retCode == 0) {
                            deferred.resolve(res);
                        }
                    });
                return deferred.promise;
            }
        }])
        .controller('expCenterListCtrl', [
            '$scope',
            '$timeout',
            'getExpCenterList',
            'resetPwdService',
            'createAccountService',
            function ($scope, $timeout, getExpCenterList, resetPwdService, createAccountService) {
                $scope.pageInfo = {
                    pageIndex: 0,
                    pageSize: 10
                };

                /**
                 * 获取体验账号列表
                 * @param pageIndex
                 */
                $scope.getList = function (pageIndex) {
                    var queryData = {
                        pageIndex: pageIndex || 0,
                        pageSize: $scope.pageInfo.pageSize
                    };
                    getExpCenterList(queryData)
                        .then(function (res) {
                            $scope.expCenterList = res.data.list;
                            $scope.total = res.data.total;
                            $('#pagination').pagination(parseInt($scope.total), {
                                current_page: $scope.pageInfo.pageIndex,
                                num_edge_entries: 1, //边缘页数
                                num_display_entries: 4, //主体页数
                                callback: function (page_index, jq) {
                                    $scope.pageInfo.pageIndex = page_index-1;
                                    $scope.getList(page_index-1);
                                },
                                items_per_page: $scope.pageInfo.pageSize
                            });
                        });
                };

                /**
                 * 重置密码
                 * @param record
                 */
                $scope.resetPwd = function (record,dialog,dialogScope) {
                    util.confirm('确认修改 '+record.username+' 的信息吗？',{
                        ok:function(){
                            var data = {
                                uid: record.uid,
                                status: dialogScope.record.status,
                                pwd: dialogScope.record.pwd
                            };
                            resetPwdService(data).then(function (res) {

                                if (res.retCode == 0) {
                                    ui.showSucTip('修改成功！');
                                    setTimeout(function(){$scope.getList()},2500);
                                } else {
                                    ui.showErrTip(res.retMsg || res.errMsg || '修改失败!');
                                }

                            });
                            dialog.close();
                        }
                    });
                    return false;
                };

                /**
                 * 创建账号
                 * @param dialog
                 * @param dialogScope
                 */
                $scope.createAccount = function (dialog, dialogScope) {
                    var data = {
                        username: dialogScope.username,
                        pwd: dialogScope.pwd
                    };
                    createAccountService(data).then(function (res) {
                        if (res.retCode == 0) {
                            ui.showSucTip('创建账号成功！');
                            $timeout(function () {
                                $scope.getList();
                            })
                        } else {
                            ui.showErrTip(res.retMsg || res.errMsg || '创建账号失败!');
                        }

                    });

                };

                $scope.onShowCreateDialog = function(dialog, dialogScope){
                    dialogScope.pwd = Math.floor(Math.random()*1000000)+'';
                };

                $scope.onShowResetPwdDialog = function(rec,dialog,dialogScope){
                    dialogScope.record = rec;
                };

                $scope.getList();

            }])
});